package manager.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//公共查询方法
public class BaseDao {
    public static ResultSet executedDQL(String sql,Object[] params){
        //获取连接
        Connection conn =DBConn.getConn();
        //创造预编译对象
        PreparedStatement st=null;
        //返回结果集对象
        ResultSet rs=null;
        //预编译
        try {

            st=conn.prepareStatement(sql);
            if(params !=null){
                for(int i=0;i<params.length;i++){
                    st.setObject((i+1),params[i]);
                }

            }
            rs=st.executeQuery();
        }catch(SQLException e){
            throw new RuntimeException(e);
        }


        return rs;
    }

    //增删改
    public static int executedDML(String sql,Object... params){
        //获取连接
        Connection conn =DBConn.getConn();
        //创造预编译对象
        PreparedStatement st=null;
        //返回结果集对象
        int r=0;
        //预编译
        try {

            st=conn.prepareStatement(sql);
            if(params !=null){
                for(int i=0;i<params.length;i++){
                    st.setObject((i+1),params[i]);
                }
            }
            r=st.executeUpdate();
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
        return r;
    }
}
